package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.MaintainPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.MaintainTwoPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IMaintain;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IMaintainTwo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.fragment.DetailFragment;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.AnimationUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.MaintainPopup;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/7/17 20:32
 * 邮箱：zhanghuaiha@gmail.com
 * 待维护门店
 */

public class MaintainTwoActivity extends BaseActivity<IMaintainTwo,MaintainTwoPresenter> implements IMaintainTwo{
    private TextView sort,page_tv,maintain_time_tv,time_left,time_right;
    private ImageView back,icon,bg;
    private MaintainPopup titlePopup;
    private TextView list_yet,list_not;
    private boolean flag = false;
    private static final int TIME = 10;
    private static final int NEXT = 101;
    private LinearLayout ll;
    private ImageView ts;
    private List<Integer> mList;
    private ViewPager viewpager;
    private int TOTAL_SIZE = 26;
    private final int PAGE_SIZE = 10;
    private int mCount = 0;
//    private MaintainPopup.OnItemOnClickListener onitemClick = type -> {
//        if (type == 0) {
//            DialogUtils.TSDialog(MaintainActivity.this,MaintainActivity.this.getString(R.string.message_dialog),true);
//        }else {
//            mPresenter.setType(type);
//        }
//        titlePopup.dismiss();
//    };

    private String time1 = DateUtils.getTime(System.currentTimeMillis());
    private int day = -1;


    public void initView() {
        sort = (TextView) findViewById(R.id.maintain_sort);
        time_left = (TextView) findViewById(R.id.time_left);
        time_right = (TextView) findViewById(R.id.time_right);
        maintain_time_tv = (TextView) findViewById(R.id.maintain_time_tv);
        back = (ImageView) findViewById(R.id.maintain_back);
        bg = (ImageView) findViewById(R.id.bg);
        ts = (ImageView) findViewById(R.id.ts);
        page_tv = (TextView) findViewById(R.id.maintain_tv);
        list_yet = (TextView) findViewById(R.id.list_yet);
        list_not = (TextView) findViewById(R.id.list_not);
        ll  = (LinearLayout) findViewById(R.id.ll);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
    }

    @Override
    public void initData() {

        String mm = DateUtils.getMM(System.currentTimeMillis());

        initPopWindow();
        mPresenter.loadContacts();
        mPresenter.loadNetWork("0","","");
//        if (Integer.parseInt(mm) >= 22) {
//            ll.setVisibility(View.VISIBLE);
//            list_yet.setSelected(false);
//            list_not.setSelected(false);
//        }else {
//            ll.setVisibility(View.GONE);
//            mPresenter.visible2gone(true);
//        }



    }



    @Override
    public void initListener() {
        back.setOnClickListener(v -> finish());

        //待维护
        list_yet.setOnClickListener(v -> {
            mPresenter.loadNetWork("0",time1,"0");
            list_yet.setSelected(true);
            list_not.setSelected(false);
        });

        //维护成功
        list_not.setOnClickListener(v -> {
            mPresenter.loadNetWork("0",DateUtils.getTime(System.currentTimeMillis()),"1");
            list_not.setSelected(true);
            list_yet.setSelected(false);
        });

        sort.setOnClickListener(v -> {
          //  titlePopup.show(findViewById(R.id.maintain_title));
            Intent intent = new Intent(MaintainTwoActivity.this,DateActivity.class);
            intent.putExtra("day",day);
            startActivityForResult(intent,NEXT);
        });


        String message = "待维护店铺如何计算的：\r\n" +
                " 1、【每晚10点】统计当日设备在线时长\r\n" +
                " 2、统计已铺店铺的每个设备在线时长都满足180分钟\r\n" +
                "      （只要一台不满足，放到第二日待维护店铺中）\r\n" +
                " 3、统计今日待维护店铺的每个设备在线时长都满足180分钟\r\n" +
                "     （都满足，标记维护成功）";
        //问号
        ts.setOnClickListener(v -> {
            DialogUtils.TSLeftDialog(MaintainTwoActivity.this,message,true);
        });
    }

    private void initPopWindow() {
        // 实例化标题栏弹窗
//        titlePopup = new MaintainPopup(this, ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//        titlePopup.setItemOnClickListener(onitemClick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == NEXT) {
            if (null != data) {
                time1 = data.getStringExtra("time");
                day = data.getIntExtra("day",-1);
                mPresenter.loadNetWork("0", time1,"");
                list_yet.setSelected(false);
                list_not.setSelected(false);
                if (DateUtils.getTime_long(time1) > System.currentTimeMillis()) {
                    ll.setVisibility(View.GONE);
                    mPresenter.visible2gone(true);
                }else if(time1.equals(DateUtils.getTime(System.currentTimeMillis()))){
                    if (Integer.parseInt(DateUtils.getMM(System.currentTimeMillis())) >= 22) {
                        ll.setVisibility(View.VISIBLE);
                        mPresenter.visible2gone(false);
                    }else {
                        ll.setVisibility(View.GONE);
                        mPresenter.visible2gone(true);
                    }
                }else {
                    ll.setVisibility(View.VISIBLE);
                    mPresenter.visible2gone(false);
                }
            }
        }
    }

    @Override
    protected MaintainTwoPresenter createPresenter() {
        return new MaintainTwoPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_maintain_two;
    }

    @Override
    public TextView getTV() {
        return page_tv;
    }

    @Override
    public TextView getLeftTime() {
        return time_left;
    }

    @Override
    public TextView getRightTime() {
        return time_right;
    }

    @Override
    public ImageView getBg() {
        return bg;
    }

    @Override
    public ViewPager getVP() {
        return viewpager;
    }


}
